package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.Battery;
import cc.yugu.yhdw.common.domain.hdw.BatteryPack;
import cc.yugu.yhdw.common.domain.hdw.Supplier;
import cc.yugu.yhdw.common.domain.hdw.SupplierInfo;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.hdw.BatteryMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.BatteryPackMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * 电池组业务
 * 
 * @Date:Created in 2018/7/5 16:26
 * @Author:
 * @Modified By: zjn 2018/7/5 16:26
 */ 
@Service
public class BatteryPackService extends AbstractService {
    @Autowired
    BatteryPackMapper batteryPackMapper;
    @Autowired
    BatteryMapper batteryMapper;
    /**
    * 分页查询
    * 
    * @Param: [batteryPack]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/5  16:31
    */ 
    public Page findPage(BatteryPack batteryPack) {
        Page page = batteryPack.buildPage();
        page.setTotalItems(batteryPackMapper.findPageCount(batteryPack));
        batteryPack.setBeginIndex(page.getOffset());

        List<BatteryPack> batteryPackList = batteryPackMapper.findPageResult(batteryPack);
        for (BatteryPack e : batteryPackList) {
            if (e.getSupplierId() != null) {
                SupplierInfo supplier = findSupplierInfo(e.getSupplierId());
                e.setSupplierName(supplier.getSupplierName());
            }
        }
        page.setResult(batteryPackList);

        return page;
    }
    /**
    * 查询
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.BatteryPack
    * @Author By: zjn 2018/7/5  16:32
    */ 
    public BatteryPack find(int id) {
        BatteryPack batteryPack = batteryPackMapper.find(id);
        // 如果厂商id不为null
        if (batteryPack.getSupplierId() != null) {
            SupplierInfo supplier = findSupplierInfo(batteryPack.getSupplierId());
            batteryPack.setSupplierName(supplier.getSupplierName());
        }
        return batteryPack;
    }
    /**
    * 插入数据
    *
    * @Param: [batteryPack]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/5  16:43
    */
    public ExtResult insert(BatteryPack batteryPack) {
        if (batteryPackMapper.checkRepeat(batteryPack) > 0) {
            return ExtResult.failResult("电池组序号已存在不能重复添加");
        }
        batteryPack.setExchangeAmount(0);
        batteryPack.setCreateTime(new Date());
        int total = batteryPackMapper.insert(batteryPack);
        if (total==0){
            return ExtResult.failResult("添加失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 更新
    *
    * @Param: [batteryPack]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/5  16:43
    */
    public ExtResult update(BatteryPack batteryPack) {
        if (!batteryPack.getPackNo().equals(batteryPackMapper.find(batteryPack.getId()).getPackNo()) && batteryPackMapper.checkRepeat(batteryPack) > 0) {
            return ExtResult.failResult("电池组序号已存在不能重复添加");
        }
        int total=batteryPackMapper.update(batteryPack);
        if (total==0){
            return ExtResult.failResult("修改失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 删除
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/5  16:43
    */
    public ExtResult delete(int id) {
        Battery battery = new Battery();
        battery.setPackId(id);
        if (batteryMapper.findPageCount(battery) > 0)
            return ExtResult.failResult("电池组下存在电池,请先删除电池再删除电池组");
        int total= batteryPackMapper.delete(id);
        if (total==0){
            return ExtResult.failResult("删除失败");
        }
        return ExtResult.successResult();
    }

}
